package cn.com.elite;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.Map;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

import cn.com.elite.util.Util;

public class EliteTread implements Runnable{
	private static final Log log=LogFactory.getLog(EliteTread .class);
	private boolean flag = true; //停止线程标记 
	private static int Hz=30; 
	private static String sql2;
	private static String sql3;
	public static cn.com.elite.EliteBean ebean;
	private static String daytime="";
	public static String getSql2() {
		return sql2;
	}
	public static void setSql2(String sql2) {
		EliteTread.sql2 = sql2;
	}
	public static String getSql3() {
		return sql3;
	}
	public static void setSql3(String sql3) {
		EliteTread.sql3 = sql3;
	}
	public static int getHz() {
		return Hz;
	}
	public static void setHz(int hz) {
		Hz = hz;
	}
	public static cn.com.elite.EliteBean getEbean() {
		return ebean;
	}
	public static void setEbean(cn.com.elite.EliteBean ebean) {
		EliteTread.ebean = ebean;
	}
	public void run(){
		log.info("-------------------------EliteTread-----------------------------");
		while(flag){
			try{
				Date date=new Date();
				String ee=String.format("%tR",date); //获取现在几点
				String dd=String.format("%tF",date); //获取现在几号
				String now1=ee.substring(0,2);
				log.info("dd:"+dd);
				if(!dd.equals(daytime)){
					log.info("------------------StartRun()----------------------");
					do_run();
					daytime=dd;
					log.info("-------------------------同步完成--------------------------");
				}else{
					log.info("------------------条件未到不同步,进程休眠"+Hz*1000+"秒----------------------");
				}
			}catch(Exception e){
				log.error("run()错误:"+Util.getERRO(e)); 
			}
			try{
            	Thread.sleep(Hz*1000);
            }catch(Exception e){
            	log.error("定时错误:"+Util.getERRO(e)); 
            }
		}
	}
	private FTPClient getFTPClient(String hostName,int port,String userName,String password){
		try{
			FTPClient ftpClient=new FTPClient();
			ftpClient.setDefaultPort(port);
			ftpClient.connect(hostName);            
	        ftpClient.login(userName,password);
	        return ftpClient;
		}catch(Exception e){
			log.error("FTPClient错误:"+Util.getERRO(e));
			return null;
		}
	}
	private FTPClient getFTP(String PARAMETERVALUE){
		try{
			if(PARAMETERVALUE!=null&&PARAMETERVALUE.length()>0){
				String[] a=PARAMETERVALUE.split(",");
				if(a!=null&&a.length>0){
					if(a.length==3){
						FTPClient ftpClient=getFTPClient(a[0],Integer.parseInt(a[1]),a[2],"");
						return ftpClient;
					}else if(a.length==4){
						FTPClient ftpClient=getFTPClient(a[0],Integer.parseInt(a[1]),a[2],a[3]);
						return ftpClient;
					}else{
						log.error("PARAMETERVALUE错误："+PARAMETERVALUE);
						return null;
					}
				}else{
					log.error("PARAMETERVALUE错误："+PARAMETERVALUE);
					return null;
				}
			}
			return null;
		}catch(Exception e){
			log.error("getFTP错误:"+Util.getERRO(e));
			return null;
		}
	}
	private void do_run1(BasicDataSource dataSource,String Project_id) throws Exception{
		Connection conn=dataSource.getConnection();
		Statement stmt=conn.createStatement();
		ResultSet rs=stmt.executeQuery(sql3);
		while(rs.next()){
			String PARAMETERVALUE=rs.getString("PARAMETERVALUE");
			FTPClient ftpClient=getFTP(PARAMETERVALUE);
			ResultSet rs1=stmt.executeQuery(sql2);
			while(rs1.next()){
				String DOCUMENTPATH=rs1.getString("DOCUMENTPATH");
				FTPFile[] ftpFiles=ftpClient.listFiles(DOCUMENTPATH);
				if(ftpFiles==null||ftpFiles.length==0){
					String RECORD_GUID=rs1.getString("RECORD_GUID");
					String DNIS=rs1.getString("DNIS");
					String ANI=rs1.getString("ANI");
					String STARTTIME=rs1.getString("STARTTIME");
					String ENDTIME=rs1.getString("ENDTIME");
					String STAFF_ID=rs1.getString("STAFF_ID");
					String AGENT_ID=rs1.getString("AGENT_ID");
					String CALL_TYPE=rs1.getString("CALLTYPE");
					String RECORDNO=rs1.getString("RECORDNO");
					String sql=" insert into record_lose(PROJECT_ID,DOCUMENTPATH,RECORD_GUID,DNIS,ANI,STARTTIME,ENDTIME,STAFF_ID,AGENT_ID,CALL_TYPE,RECORDNO)" +
							"Values('"+Project_id+"','"+DOCUMENTPATH+"','"+RECORD_GUID+"','"+DNIS+"','"+ANI+"','"+STARTTIME+"','"+ENDTIME+"','"+STAFF_ID+"','"+AGENT_ID+"','"+CALL_TYPE+"','"+DOCUMENTPATH+"')  ";
					int result=stmt.executeUpdate(sql);
				}
			}
			ftpClient.disconnect();
			rs1.close();
		}
		rs.close();
		stmt.close();
		conn.close();
		
	}
	private void do_run() throws Exception{
		if(StartThread.sql_list!=null){
			for(int i=0;i<StartThread.sql_list.size();i++){
				Map map=(Map)StartThread.sql_list.get(i);
				BasicDataSource dataSource=(BasicDataSource)map.get("dataSource");
				String PROJECT_ID=(String)map.get("PROJECT_ID");
				do_run1(dataSource,PROJECT_ID);
			}
		}
	}
}
